;(function($,w,doc) {
    // ele弹窗对象,trigger触发显示弹窗jq对象[必需]，cancel取消弹窗对象，option配置参数，callback回调
    var float = function (ele, trigger, cancel, option, callback) {
        var that = this,
            // 居中box的样式
            style = {
                'position': 'absolute',
                'left': '50%',
                'top': '50%',
                'height': '345px',
                'width': '456px',
                'z-index': 999999
            },
            config = {
                // 隐藏的class
                'hideClass': 'dn',
                // 遮罩的样式
                'maskDefaultStyle': {
                    'position': 'absolute',
                    'top': '0',
                    'bottom': '0',
                    'left': '0',
                    'right': '0',
                    'z-index': 999998
                },
                'maskStyle': {
                    'background': '#000',
                    'opacity': '.3'
                },
                // 自定义遮罩的class
                'maskClass': 'float-layer-mask',
                // 遮罩添加的层级，以div.class等格式
                'putMaskWrap': 'body',
                'eventType': 'click',
                'maskClickCancel': true
            };
        that.ele = ele;
        that.style = style;
        that.callback = callback;
        var options = that.options = $.extend({}, style, config, option);
        that.initSet(that);

        // 获取计算后的宽高
        var aSize = that.getSize(that.ele);
        that.getheight = aSize[0];
        that.getWidth = aSize[1];
        // 获取计算后的偏离中心位移
        var aPosition = that.getPosition(aSize);
        that.getTop = aPosition[0];
        that.getLeft = aPosition[1];
        that.setPosition(that);

        // 事件绑定
        trigger.on(options.eventType,function () {
            that.showFloat(that);
        });
        // 取消弹窗,合并存在且选择为取消弹窗的数组对象
        var aCancel = [];
        if(cancel.length > 0) {
            aCancel.push.apply(aCancel,cancel);
        }
        if(options.maskClickCancel) {
            aCancel.push($('.'+options.maskClass));
        }
        $(aCancel).each(function() {
            $(this).on('click',function () {
                that.hideFloat(that);
                if(callback) {
                    that.callback();
                }
            });
        });
    };

    float.prototype = {
        // 初始化遮罩，弹框样式
        initSet: function(that) {
            // 弹窗box
            var option = that.options;
            that.ele.css(that.style).addClass(option.hideClass);
            // 如果不存在该遮罩，创建并添加mask
            if($('.' + option.maskClass).length === 0) {
                var oMask = $('<div class=' + option.maskClass +'></div>');
                // mask的样式
                var style = $.extend({},option.maskStyle,option.maskDefaultStyle);
                oMask.css(style).addClass(option.hideClass);
                $(option.putMaskWrap).append(oMask)
            }
        },
        // 获得元素宽高
        getSize: function(ele) {
            return [ele.outerHeight(true),ele.outerWidth(true)];
        },
        // 获得元素宽高一半
        getPosition: function(aSize) {
            return [Math.round(aSize[0]/2),Math.round(aSize[1]/2)];
        },
        // 设置元素偏移量
        setPosition: function(that) {
            that.ele.css({'margin-top': -that.getTop,'margin-left': -that.getLeft});
        },
        showFloat: function(that) {
            $('.'+ that.options.maskClass).removeClass('dn');
            that.ele.removeClass('dn');
        },
        hideFloat: function(that) {
            $('.'+ that.options.maskClass).addClass('dn');
            that.ele.addClass('dn');
        }
    };
    // 注册到JQ
    $.fn.floatBox = function(trigger, cancel, option, callback) {
                    this.each(function() {
                        new float($(this), trigger, cancel, option, callback);
                    });
                    return this;
                };
    w.float = float;
})(jQuery,window,document);